package com.css.apps.base.user.action.login;

import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.UserAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.util.StringHelper;

@SuppressWarnings("serial")
public class ChangeUserOrg extends UserAction {

	private Log log = LogFactory.getLog(getClass());
	private String orgId;
	
	@SuppressWarnings("unchecked")
	@Override
	protected String userGo() {
		try {
			if(StringHelper.isEmpty(orgId)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			SUser user = (SUser) sUser;
			JSONObject org = null;
			if(user != null) {
				user.setOrgId(orgId);
				ActionContext.getContext().getSession().put(Environment.SESSION_LOGIN_KEY, user);
				org =  new JSONObject();
				org.put("orgName", QueryCache.get(SOrg.class,orgId).getName());
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), org);
			return Action.SUCCESS;
		} catch (Exception ex) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage());
			return Action.ERROR;
		}
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

}
